/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 12.08.2006
 *
 *************************************************/
package fightplan.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import fightplan.business.Category;
import fightplan.business.TournamentHolder;

@SuppressWarnings("serial")
public class DrawingOptionsPanel extends JPanel {
	private Category category;
	private JCheckBox cbSpecialName;
	private JTextField tName;
	private JRadioButton rbNormal;
	private JRadioButton rbFinalrunde;
	private JCheckBox cbMin2Sheets;
	private JCheckBox cbTrostrunde;
	private JLabel labelStatus;
	private JLabel labelPlanTime;
	private JLabel labelPrintTime;

	public DrawingOptionsPanel(final TournamentHolder tournamentHolder) {
		
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(5000, 5000));

		rbNormal = new JRadioButton("Normales Cupsystem");
		rbNormal.setSelected(true);
		cbMin2Sheets = new JCheckBox("Mindestens 2 Blätter generieren");
		cbMin2Sheets.setSelected(true);
		cbTrostrunde = new JCheckBox("Trostrunde generieren");
		cbTrostrunde.setSelected(true);
		rbFinalrunde = new JRadioButton("Finalrunde (Zusammengesetzt aus Blattsieger und Trostrundensieger)");
		rbFinalrunde.setSelected(false);

		FormLayout layout = new FormLayout("left:max(40dlu;p), 10dlu, pref:grow(1)");

		DefaultFormBuilder builder = new DefaultFormBuilder(layout);
		builder.setDefaultDialogBorder();

		builder.appendSeparator("Name");
		builder.append(cbSpecialName = new JCheckBox("Name nicht automatisch, sondern:"), 3);
		builder.append(tName = new JTextField(), 3);
		tName.setEditable(false);

		builder.appendSeparator("Status");
		builder.append("Status", labelStatus = new JLabel());
		builder.append("Zeit Auslosung", labelPlanTime = new JLabel());
		builder.append("Zeit Druck", labelPrintTime = new JLabel());

		builder.appendSeparator("Optionen");

		builder.append(rbNormal, 3);
		builder.append(cbTrostrunde, 3);
		builder.append(cbMin2Sheets, 3);

		builder.append(rbFinalrunde, 3);

		add(builder.getPanel(), BorderLayout.CENTER);

		cbSpecialName.addItemListener(new ItemListener() {

			public void itemStateChanged(ItemEvent e) {
				tName.setEditable(cbSpecialName.isSelected());
				if (category != null) {
					tName.setText(category.getDescription());
				}
			}
		});
		tName.getDocument().addDocumentListener(new DocumentListener() {

			public void changedUpdate(DocumentEvent e) {
				writeBack();
			}

			public void insertUpdate(DocumentEvent e) {
				writeBack();
			}

			public void removeUpdate(DocumentEvent e) {
				writeBack();
			}

			private void writeBack() {
				// check for no category selected
				if (category != null) {
					category.setName(tName.getText());
				}
			}
		});

		// no binding, simple listening
		rbNormal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (category != null) {
					if (rbNormal.isSelected()) {
						rbFinalrunde.setSelected(false);
						category.setFinalrunde(false);
						cbTrostrunde.setEnabled(true);
						cbMin2Sheets.setEnabled(true);
					}
				}
			}
		});
		rbFinalrunde.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (category != null) {
					if (rbFinalrunde.isSelected()) {
						rbNormal.setSelected(false);
						category.setFinalrunde(true);
						category.setTrostrunde(true);
						category.setMin2Sheets(true);
						cbTrostrunde.setEnabled(false);
						cbMin2Sheets.setEnabled(false);
						cbTrostrunde.setSelected(true);
						cbMin2Sheets.setSelected(true);
					}
				}
			}
		});

		cbTrostrunde.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (category != null) {
					category.setTrostrunde(cbTrostrunde.isSelected());
					tournamentHolder.fireChange();
				}
			}
		});
		cbMin2Sheets.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (category != null) {
					category.setMin2Sheets(cbMin2Sheets.isSelected());
					tournamentHolder.fireChange();
				}
			}
		});

		setCategory(null);
	}

	/*
	 * public boolean trostrunde() { return cbTrostrunde.isSelected(); }
	 * 
	 * public boolean min2Sheets() { return cbMin2Sheets.isSelected(); }
	 */

	public void setCategory(Category category) {
		this.category = category;
		if (category != null) {
			cbSpecialName.setEnabled(true);
			String name = category.getNameOrDescription();
			boolean hasSpecialName = category.hasSpecialName();
			cbSpecialName.setSelected(hasSpecialName);
			tName.setText(name);

			labelStatus.setText(category.getStatusText());
			labelPlanTime.setText(category.getPlanTime());
			labelPrintTime.setText(category.getPrintTime());

			rbNormal.setSelected(!category.isFinalrunde());
			cbTrostrunde.setSelected(category.isTrostrunde());
			cbMin2Sheets.setSelected(category.isMin2Sheets());
			rbFinalrunde.setSelected(category.isFinalrunde());

			rbNormal.setEnabled(!category.isPlanned());
			cbTrostrunde.setEnabled(!category.isPlanned() && !category.isFinalrunde());
			cbMin2Sheets.setEnabled(!category.isPlanned() && !category.isFinalrunde());
			rbFinalrunde.setEnabled(!category.isPlanned());
		} else {
			cbSpecialName.setSelected(false);
			cbSpecialName.setEnabled(false);

			tName.setText("");
			labelStatus.setText("");
			labelPlanTime.setText("");
			labelPrintTime.setText("");

			rbNormal.setEnabled(false);
			cbTrostrunde.setEnabled(false);
			cbMin2Sheets.setEnabled(false);
			rbFinalrunde.setEnabled(false);
		}
	}

}
